package thread.callable;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author cyt
 * @create 2020-03-06 8:45
 */

/**
 * 泛型中只能添加包装类，而不是添加一般的数据类型。
 *
 * @author Administrator
 */
public class CallableThread implements Callable<Integer> {
    private Integer total = 0;

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        FutureTask<Integer> future = new FutureTask<Integer>(new CallableThread());
        Thread thread = new Thread(future);
        thread.start();
        System.out.println("返回值： " + future.get());
    }

    @Override
    public Integer call() throws Exception {
        for (int i = 2; i < 100; i++) {
            boolean flag = true;
            for (int j = 2; j < i - 1; j++)
                if (i % j == 0) {
                    flag = false;
                    break;
                }

            if (flag) {
                total += i;
            }
        }

        return total;
    }

}
